import { Type } from "class-transformer";
import type { YAMLActivity, YAMLBlueprints } from "../index";
import type { UniverseType } from "./UniverseType";

export class ActivityMaterialItem {
  // 数量
  quantity!: number;
  // 类型ID
  id!: number;
  name!: string;
}

class SkillLevel {
  // 等级
  level!: number;
  // 类型ID
  id!: number;
  name!: string;
}
class Activity {
  // 输入
  @Type(() => ActivityMaterialItem)
  materials?: ActivityMaterialItem[];
  // 产出
  @Type(() => ActivityMaterialItem)
  products?: ActivityMaterialItem[];
  // 制造时间
  time!: number;
  //所需技能
  @Type(() => SkillLevel)
  skills?: SkillLevel[];

  static formYaml(yaml: YAMLActivity, types: Map<number, UniverseType>) {
    const activity = new Activity();
    activity.time = yaml.time;
    const filterNotExistType = (e: any) => types.get(e.typeID);
    if (yaml.materials) {
      activity.materials = yaml.materials.filter(filterNotExistType).map((item) => {
        return {
          quantity: item.quantity,
          id: item.typeID,
          name: types.get(item.typeID)!.name,
        };
      });
    }
    if (yaml.products) {
      activity.products = yaml.products.filter(filterNotExistType).map((item) => {
        return {
          quantity: item.quantity,
          id: item.typeID,
          name: types.get(item.typeID)!.name,
        };
      });
    }
    if (yaml.skills) {
      activity.skills = yaml.skills.filter(filterNotExistType).map((item) => {
        return {
          level: item.level,
          id: item.typeID,
          name: types.get(item.typeID)!.name,
        };
      });
    }
    return activity;
  }
}

/**
 * 蓝图
 */
export class BluePrint {
  // 蓝图类型ID
  id!: number;
  name!: string;
  // 最大生产流程数量
  maxProductionLimit!: number;
  @Type(() => Activity)
  copying?: Activity;
  //发明
  @Type(() => Activity)
  invention?: Activity;
  //反应
  @Type(() => Activity)
  reaction?: Activity;
  //制造
  @Type(() => Activity)
  manufacturing?: Activity;
  //蓝图材料研究
  @Type(() => Activity)
  research_material?: Activity;
  //蓝图时间研究
  @Type(() => Activity)
  research_time?: Activity;

  type?: UniverseType;

  static fromYaml(yaml: YAMLBlueprints, types: Map<number, UniverseType>) {
    const blueprint = new BluePrint();
    blueprint.id = yaml.blueprintTypeID;
    blueprint.name = types.get(yaml.blueprintTypeID)!.name;
    blueprint.maxProductionLimit = yaml.maxProductionLimit;
    if (yaml.activities.copying) {
      blueprint.copying = Activity.formYaml(yaml.activities.copying, types);
    }
    if (yaml.activities.invention) {
      blueprint.invention = Activity.formYaml(yaml.activities.invention, types);
    }
    if (yaml.activities.reaction) {
      blueprint.reaction = Activity.formYaml(yaml.activities.reaction, types);
    }
    if (yaml.activities.manufacturing) {
      blueprint.manufacturing = Activity.formYaml(yaml.activities.manufacturing, types);
    }
    if (yaml.activities.research_material) {
      blueprint.research_material = Activity.formYaml(yaml.activities.research_material, types);
    }
    if (yaml.activities.research_time) {
      blueprint.research_time = Activity.formYaml(yaml.activities.research_time, types);
    }
    return blueprint;
  }
}
